/**
 *  Copyright (C) 2011  Thomas ROBIN
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package triseries;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/**
 * @author tshepo
 * 
 */
public class MoveFile extends Thread {

	private static final boolean DEBUG = (Main.debug || MyProperties.debug.equalsIgnoreCase("MoveFile"));
	public static boolean override = false;

	File source;
	File destination;

	MoveFile(String s, String d) {
		source = new File(s);
		destination = new File(d);
	}

	public static boolean copieFile(File source, File dest) {
		if(DEBUG) System.err.println("MoveFile.copieFile("+source.getName()+","+dest.getName()+")");

		FileChannel in = null; // canal d'entrée
		FileChannel out = null; // canal de sortie

		boolean ok = true;

		// Init
		try {
			in = new FileInputStream(source).getChannel();
			out = new FileOutputStream(dest).getChannel();
		} catch (FileNotFoundException e1) {
			Main.processExeption("MoveFile.copieFile :\n"
					+ e1.getStackTrace().toString());
			ok = false;
		}
		// Copie depuis le in vers le out
		try {
			in.transferTo(0, in.size(), out);
		} catch (IOException e1) {
			Main.processExeption("MoveFile.copieFile :\n"
					+ e1.getStackTrace().toString());
			ok = false;
		}

		if (in != null) {
			try {
				in.close();
			} catch (IOException e) {
				Main.processExeption("MoveFile.copieFile :\n"
						+ e.getStackTrace().toString());
			}
		}
		if (out != null) {
			try {
				out.close();
			} catch (IOException e) {
				Main.processExeption("MoveFile.copieFile :\n"
						+ e.getStackTrace().toString());
			}
		}
		return (ok);
	}

	@Override
	public void run() {
		if(DEBUG) System.err.println("MoveFile.run()");

		boolean result = false;
		String s = "";
		if (destination.exists()) {
			if (override) {
				Main.processWarning(source.getName()
						+ " écrase un fichier existant");
			} else {
				s = source.getName()
						+ " existe déjà\nVous pouvez ajouter \"Override=true\""
						+ " dans le fichier de settings pour remplacer automatiquement les fichiers déjà existants";
				Main.processExeption(s);
				return;
			}
		}
		destination.getParentFile().mkdirs();
		// On essaye avec renameTo
		result = source.renameTo(destination);
		if (!result) {
			// On essaye de copier
			result = copieFile(source, destination);
			if (result) {
				result &= source.delete();
			}

		}
		if (!result) {
			s = "Une erreur est survenue lors du déplacement de " + "\n" + s;
			Main.processExeption(s);
		}
	}
}
